/**
 * 表单组件实例化工厂，根据表单字段配置获取表单字段组件实例
 */

import * as elFormComponentsMap from 'element-plus';
import imageList from '../components/configAbleForm/components/imageList.vue';

export const getComponentSize = (elItem: elItemType) => {
    if (elItem === 'ElTextarea') {
        return elFormComponentsMap['ElInput'];
    }
    if (elItem === 'ElImgList') {
        return imageList;
    }
    if (elItem === 'ElDateTimePicker') {
        return;
    }
    return elFormComponentsMap[elItem];
};

export const dealFormItemConfig = (formItemConfigs: formItemConfigType[]) => {
    let formConfigs: formItemConfigType[] = [];
    // 预处理formConfig
    formItemConfigs.forEach((item: formItemConfigType) => {
        let attributesBind: any;
        switch (item.elItem) {
            case 'ElInputNumber':
                attributesBind = Object.assign(
                    {},
                    {
                        step: 1
                    },
                    item.attributesBind
                );
                formConfigs.push(Object.assign({}, item, { attributesBind }));
                break;
            case 'ElInput':
                attributesBind = Object.assign(
                    {},
                    {
                        style: 'width: 240px'
                    },
                    item.attributesBind
                );
                formConfigs.push(Object.assign({}, item, { attributesBind }));
                break;
            case 'ElSelect':
                attributesBind = Object.assign(
                    {},
                    {
                        style: 'width: 240px'
                    },
                    item.attributesBind
                );
                formConfigs.push(Object.assign({}, item, { attributesBind }));
                break;
            case 'ElDatePicker':
                let formatDate = item.attributesBind?.type?.includes('time')
                    ? 'YYYY-MM-DD hh:mm:ss'
                    : 'YYYY-MM-DD';
                attributesBind = Object.assign(
                    {},
                    {
                        type: 'date',
                        placeholder: '选择时间',
                        format: formatDate,
                        'value-format': formatDate
                    },
                    item.attributesBind
                );
                formConfigs.push(Object.assign({}, item, { attributesBind }));
                break;
            case 'ElTextarea':
                attributesBind = Object.assign(
                    {},
                    {
                        type: 'textarea',
                        autosize: { minRows: 2, maxRows: 4 }
                    },
                    item.attributesBind
                );
                formConfigs.push(Object.assign({}, item, { attributesBind }));
                break;
            case 'ElUpload':
                attributesBind = Object.assign(
                    {},
                    {
                        drag: true,
                        multiple: true,
                        accept: '*',
                        'auto-upload': false
                    },
                    item.attributesBind
                );
                formConfigs.push(Object.assign({}, item, { attributesBind }));
                break;
            case 'ElTreeSelect':
                attributesBind = Object.assign(
                    {},
                    {
                        data: item.optionConfigs,
                        style: 'width: 240px',
                        'check-strictly': true,
                        'render-after-expand': false,
                        'show-checkbox': true
                    },
                    item.attributesBind
                );
                formConfigs.push(Object.assign({}, item, { attributesBind }));
                break;
            default:
                formConfigs.push(item);
                break;
        }
    });
    return formConfigs;
};

export function getRules(formItemConfigs: formItemConfigType[], isValidateRequire: boolean = true) {
    let rules: EmptyObjectType = {};
    formItemConfigs.forEach((item: formItemConfigType) => {
        if (!isValidateRequire) {
            rules[item.prop] = item.fieldRule?.filter((ruleItem) => !ruleItem.required);
        } else {
            rules[item.prop] = item.fieldRule;
        }
    });
    return rules;
}
